package com.bojiu.lawsearch.lawsearch.controller;

import com.bojiu.lawsearch.lawsearch.model.Page;
import com.bojiu.lawsearch.lawsearch.model.Setting;
import com.bojiu.lawsearch.lawsearch.model.WechatUser;
import com.bojiu.lawsearch.lawsearch.service.BackUserService;
import com.bojiu.lawsearch.lawsearch.util.AuthorizationHandle;
import com.bojiu.lawsearch.lawsearch.util.Constances;
import com.bojiu.lawsearch.lawsearch.util.JsonResult;
import com.bojiu.lawsearch.lawsearch.util.ResultCode;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: afdiao
 * @Date：2020/3/8 18:09
 * @Description:
 */
@Api(description = "后台管理员操作应用端用户，已完成！！！")
@RestController
@RequestMapping("backUser")
@Slf4j
public class BackUserController {

    @Autowired
    BackUserService backUserService;

    @Autowired
    AuthorizationHandle authorizationHandle;


    /**
     * 后台获取用户列表接口
     *
     * @param pageNum
     * @param pageSize
     * @param string
     * @param request
     * @return
     */
    @ApiOperation(value = "获取用户列表", notes = "用户列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = WechatUser.class),
    })
    @PostMapping("list/{pageNum}/{pageSize}")
    public String getList(@ApiParam(name = "pageNum", value = "当前页码", required = true)@PathVariable("pageNum") int pageNum, @ApiParam(name = "pageSize", value = "每页个数", required = true)@PathVariable("pageSize") int pageSize, @ApiParam(name = "name  昵称；type 类型  0表示普通用户 1表示工程师；", value = "查询条件，json格式", required = true)@RequestBody String string, HttpServletRequest request) {
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(string, JsonObject.class);
        //获取查询条件
        String key = jsonObject.has("name") ? jsonObject.get("name").getAsString() : null;
        JsonElement type = jsonObject.has("type") ? jsonObject.get("type") : null;
        String typeValue = null;
        if (!type.isJsonNull()) {
            typeValue = type.getAsString();
        }
        String province = jsonObject.has("province") ? jsonObject.get("province").getAsString() : null;
        String city = jsonObject.has("city") ? jsonObject.get("city").getAsString() : null;
        String label_id = jsonObject.has("label_id") ? jsonObject.get("label_id").getAsString() : null;
        Page<WechatUser> userList = backUserService.getUserList(key, typeValue, province, city, label_id, pageNum, pageSize);
        return gson.toJson(new JsonResult(ResultCode.SUCCESS, userList));
    }




    /**
     * 删除用户
     * @param id
     * @param request
     * @return
     */
    @ApiOperation(value = "删除用户", notes = "删除指定的用户")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = WechatUser.class),
    })
    @GetMapping("delete/{id}")
    public String delete(@ApiParam(name = "id", value = "用户id", required = true)@PathVariable("id") int id, HttpServletRequest request) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
        if (!authon.getCode().equals("200")) {
            return new Gson().toJson(authon);
        }
        backUserService.deleteById(id);
        return new Gson().toJson(new JsonResult(ResultCode.SUCCESS));
    }

    /**
     * 获取工程师表
     * @param pageNum
     * @param pageSize
     * @param string
     * @param request
     * @return
     */
    @ApiOperation(value = "获取工程师列表", notes = "删除指定的用户")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = WechatUser.class),
    })
    @PostMapping("engineer/list/{pageNum}/{pageSize}")
    public String engineerList(@ApiParam(name = "pageNum", value = "当前页码", required = true)@PathVariable("pageNum") int pageNum, @ApiParam(name = "pageSize", value = "每页个数", required = true)@PathVariable("pageSize") int pageSize,@ApiParam(name = "state", value = "工程师是否认证  未认证0，认证1，认证未通过2", required = true) @RequestBody String string, HttpServletRequest request) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
        if (!authon.getCode().equals("200")) {
            return new Gson().toJson(authon);
        }
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(string, JsonObject.class);
        String state = jsonObject.has("state") ? jsonObject.get("state").getAsString() : null;
        Page<WechatUser> engineerList = backUserService.getEngineerList(pageNum, pageSize, state);
        return gson.toJson(new JsonResult(ResultCode.SUCCESS, engineerList));
    }

    /**
     * 处理工程师申请
     * @param id
     * @param string
     * @param request
     * @return
     */
    @ApiOperation(value = "处理工程师申请", notes = "处理工程师申请")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = WechatUser.class),
    })
    @PostMapping("engineer/handle/{id}")
    public String handleEngineer(@PathVariable("id") int id, @RequestBody String string, HttpServletRequest request) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
        if (!authon.getCode().equals("200")) {
            return new Gson().toJson(authon);
        }
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(string, JsonObject.class);
        int state = jsonObject.get("state").getAsInt();
        String reason = jsonObject.has("reason") ? jsonObject.get("reason").getAsString() : null;
        if (2 == state) {
            if (StringUtils.isEmpty(reason)) {
                log.error("未同意请说明理由");
            }
        }
        backUserService.handleEngineer(id, state, reason);
        return gson.toJson(new JsonResult(ResultCode.SUCCESS));
    }
}
